package com.gtm.controller;

import com.alibaba.fastjson.JSONObject;
import com.gtm.model.Group;
import com.gtm.model.Pager;
import com.gtm.model.User;
import com.gtm.repository.GroupRepository;
import com.gtm.service.GroupService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by ryan on 2017/12/5.
 */
@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService gs;

    @Autowired
    private GroupRepository gr;

    @ApiOperation(value="findById", notes="通过id查找",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "rid",value = "id",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/findById/{gid}")
    public Group findById(@PathVariable String gid){
        return gr.findOne(gid);
    }

    @ApiOperation(value="listWithKeyMeta", notes="究极查询",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "size",value = "行数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "page",value = "页数",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "searchKey",value = "条件",dataType = "String",paramType = "body")
    } )
    @RequiresRoles(value = {"teacher","student","director"},logical = Logical.OR)
    @RequestMapping(value = {"/listWithKeyMeta/{size}/{page}"})
    Page<Group> listWithKeyMeta(@PathVariable Integer size, @PathVariable Integer page, @RequestBody(required = false) String searchKey){
        return gs.listWithKeyMeta(new PageRequest(page,size, Sort.Direction.DESC,"createDate"),searchKey);
    }

    @ApiOperation(value="deleteById", notes="通过id删除",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "gid",value = "id",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/deleteById/{gid}")
    public void deleteById(@PathVariable String gid){
         gr.delete(gid);
    }

    @ApiOperation(value="addGroup", notes="新建分组",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "address",value = "地址",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "host",value = "主持人",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "date",value = "答辩时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="countType",value = "答辩次数",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "分组类型，0为开题分组，1为论文答辩分组",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "record",value = "记录 为存储各个学生对应的评阅老师",dataType = "String",paramType = "query")
    } )
    @RequestMapping("/addGroup")
    public Group addGroup(@RequestParam("address")String address, @RequestParam("host")String host, @RequestParam("date")Date date,
                          @RequestParam("countType")String countType,@RequestParam("type")String type,@RequestParam("record")String record){
        return gs.addGroup(address,host,date,countType,type,record);
    }

    @ApiOperation(value = "listMyStu",notes = "列出评阅老师所要评阅的学生",httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "tid",value = "老师id",dataType = "String",paramType = "path")
    })
    @RequestMapping("/listMyStu/{tid}/{type}")
    public List<User> listMyStu(@PathVariable String tid,@PathVariable String type){
        return gs.listMyStu(tid,type);
    }

    @ApiOperation(value = "audit",notes = "审核打分",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid",value = "学生id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "grade",value = "成绩",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "isPass",value = "是否通过",dataType = "Boolean",paramType = "query")
    })
    @RequestMapping("/audit/{sid}")
    public User audit(@PathVariable String sid,@RequestParam("grade")String grade,@RequestParam("isPass")Boolean isPass){
        return gs.audit(sid,grade,isPass);
    }


    @ApiOperation(value="editGroup", notes="修改分组",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "id",value = "groupid",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "address",value = "地址",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "host",value = "主持人",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "date",value = "答辩时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="countType",value = "答辩次数",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "分组类型，0为开题分组，1为论文答辩分组",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "record",value = "记录 为存储各个学生对应的评阅老师",dataType = "String",paramType = "query")
    } )
    @RequestMapping("/editGroup")
    public Group editGroup(@RequestParam("id")String gid,@RequestParam("address")String address, @RequestParam("host")String host, @RequestParam("date")Date date,
                          @RequestParam(value = "countType",required = false)String countType,@RequestParam(value = "type",required = false)String type,@RequestParam("record")String record){
        return gs.editGroup(gid,address,host,date,countType,type,record);
    }

}
